package org.jasig.cas.client.util;

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

/**
* @ClassName: JedisSenPool.java
* @Description:哨兵模式redis操作
*
* @version: v1.0.0
* @author: shaolinyu
* @date: 2020年4月21日 上午10:48:07
 */
public class JedisSenPool {
	

	private static JedisSentinelPool redisSentinelJedisPool = null;
	 
    public JedisSenPool() {
        if (null == redisSentinelJedisPool) {
            initPool();
        }
    }
 
    private void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(CacheConfig.REDIS_POOL_MAX_ACTIVE);
        config.setMaxIdle(CacheConfig.REDIS_POOL_MAX_IDLE);
        config.setMaxWaitMillis(CacheConfig.REDIS_POOL_MAX_WAIT);
        // 在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的
        config.setTestOnBorrow(true);
        //在return给pool时，是否提前进行validate操作
        config.setTestOnReturn(true);
        //在空闲时检查有效性，默认false
        config.setTestWhileIdle(true);
        // 哨兵模式
        String sentinelsStr = CacheConfig.REDIS_SENTINEL_NODES;
        Set<String> sentinels = new HashSet<String>();
        for (String sentinel : sentinelsStr.split(",")) {
            sentinels.add(sentinel);
        }
        redisSentinelJedisPool = new JedisSentinelPool(CacheConfig.REDIS_SENTINEL_MASTER, sentinels, config);
    }
 
    /**
     * 同步获取Jedis实例
     *
     * @return
     */
    public synchronized  static Jedis getJedis() {
        return redisSentinelJedisPool.getResource();
    }
 
 
    public String get(String key) {
        Jedis jedis = getJedis();
        if (jedis == null) {
            System.out.println("Jedis实例获取失败！");
            throw new RuntimeException();
        }
 
        try {
            return jedis.get(key);
        } catch (Exception e) {
            System.out.println("获取值失败：" + e.getMessage());
            throw new RuntimeException();
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }
 
 
   
    public void set(String key, String value) {
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            System.out.println("设置值失败：" + e.getMessage());
            throw new RuntimeException();
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }
 
    public void set(String key, String value, int second) {
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
            jedis.expire(key, second);
        } catch (Exception e) {
            System.out.println("设置值失败：" + e.getMessage());
            throw new RuntimeException();
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }
 
 
    public void del(String key) {
        Jedis jedis = getJedis();
        try {
            jedis.del(key);
        } catch (Exception e) {
            System.out.println("删除失败：" + e.getMessage());
            throw new RuntimeException();
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }

}



